package com.danielepelagatti.flickrmanager.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.webapis.flickr.AuthPerm;
	import com.adobe.webapis.flickr.FlickrService;
	import com.adobe.webapis.flickr.Photo;
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	import com.danielepelagatti.flickrmanager.events.FlickrEvent;
	import com.danielepelagatti.flickrmanager.events.PhotoEvent;
	import com.danielepelagatti.flickrmanager.events.TagsEvent;
	import com.danielepelagatti.flickrmanager.model.Flickr;
	import com.theriabook.utils.Logger;
	
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import flash.events.ProgressEvent;
	import com.danielepelagatti.utils.URLLoaderBaseTracker;
	/**
	 * 
	 * @author dpelagatti
	 * 
	 */	
	public class FlickrCommand implements ICommand
	{
		protected var flickr:FlickrService;
		/**
		 * 
		 * @param event
		 * 
		 */		
		public function execute( event:CairngormEvent ):void
		{
			var flickrEvent:FlickrEvent = FlickrEvent(event);
			
			flickr 					= new FlickrService(Flickr.apiKey);
			flickr.secret 			= Flickr.secret;
			flickr.permission 		= AuthPerm.DELETE;
			flickr.token			= Flickr.token;
			
			if(event is PhotoEvent || event is TagsEvent)
			{
				var p:Photo = event.data as Photo;
				
				if(p)
				{
					if( CommandHelper.isOperationDone(event.type,p.id) )
					{
						Logger.debug("FlickrCommand:: Already done Photo Operation  [ "+event.type+" ] ["+p.id+"] , skipping");
						return;
					}
					CommandHelper.addPhotoOperation(event.type,p);					
				}
			}
			URLLoaderBaseTracker.getInstance().addURLLoader(flickr,event.type);
			//Application.application.status = "Executing "+event.type;
			Logger.info("FlickrCommand:: Executing "+event.type);
			flickr.addEventListener(ProgressEvent.PROGRESS,progress);
			flickr.addEventListener(event.type,result);
			IFlickrCommand(this).getAPICall().apply(this,flickrEvent.apiParams);
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function result( event:FlickrResultEvent ):void
		{
			flickr.removeEventListener(event.type,result);
			
			if( event.success == false )
			{
				var isErrorHandled:Boolean = IFlickrCommand( this ).error( event );
				
				if(!isErrorHandled)
				{
					var msg:String = "";
					if(event.data.error)
						msg = event.data.error.errorMessage;
					else
						"bo";
					
					Alert.show("Unhandled Error:\n"+msg+"\nWhile executing "+event.type,"Error");
				}
				
				Logger.error("FlickrCommand:: Error in the result of "+event.type+" error: "+msg);
				Application.application.status = "Error in the result of "+event.type+" error: "+msg;
			}
			else
			{
				Logger.info("FlickrCommand:: "+event.type+" Executing success callback");
				IFlickrCommand( this ).success( event );
				Logger.info("FlickrCommand:: "+event.type+" Completed");
				//Application.application.status = event.type+" Completed";
			}
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function progress(event:ProgressEvent):void
		{
			//Application.application.status = event.type+ " | "+event.bytesLoaded+" : "+event.bytesTotal;
		}
	}
}